#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <zlib.h>

#include "types.h"
#include "endian.h"
#include "kirk_engine.h"
#include "psp_headers.h"

// 5MB application
unsigned char pspHeader_big[336] =
{
	0x7E, 0x50, 0x53, 0x50, 0x00, 0x02, 0x00, 0x00, 0x01, 0x01, 0x22, 0x74, 0x69, 0x66, 0x70, 0x73,
	0x70, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x33, 0x55, 0x00, 0x50, 0x34, 0x55, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x40, 0x67, 0x3D, 0x00, 0x50, 0x55, 0x0A, 0x01, 0x10, 0x00, 0x40, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x6B, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x4C, 0x6B, 0x3D, 0x00, 0xCC, 0xBB, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00,
	0x90, 0x82, 0x4C, 0x48, 0xA3, 0x53, 0xB2, 0x1B, 0x13, 0x95, 0x2F, 0xF1, 0x0B, 0x90, 0x9C, 0x11,
	0x61, 0x40, 0x20, 0x67, 0xF8, 0xDB, 0xFC, 0x95, 0x5C, 0xBE, 0x8C, 0x80, 0xF3, 0x92, 0x03, 0x01,
	0xB0, 0xBE, 0xF5, 0xF8, 0xA1, 0xAF, 0xAF, 0xA8, 0x38, 0x26, 0x63, 0x09, 0x26, 0x0E, 0xB7, 0xD5,
	0x00, 0x33, 0x55, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x5C, 0x3E, 0x03, 0x22, 0xE5, 0x7D, 0xB9, 0xD1, 0x13, 0x67, 0x97, 0xA3, 0x5B, 0xD8, 0x77, 0x1F,
	0xF0, 0x05, 0xF3, 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x4A, 0xD7, 0x37,
	0xC2, 0x8F, 0x15, 0x43, 0x33, 0x93, 0x4D, 0x5B, 0xC0, 0x6E, 0xE4, 0x00, 0xC6, 0x0A, 0x71, 0x11,
	0x98, 0xB6, 0xC3, 0xB7, 0x59, 0x66, 0x21, 0xA8, 0x65, 0xF6, 0x53, 0xA9, 0x7A, 0x48, 0x17, 0xB6,
};

unsigned char kirkHeader_big[272] =
{
    0x2A, 0x4F, 0x3C, 0x49, 0x8A, 0x73, 0x4E, 0xD1, 0xF4, 0x55, 0x93, 0x0B, 0x9B, 0x69, 0xDC, 0x65,
    0x73, 0x22, 0x69, 0xD3, 0x73, 0x96, 0x7A, 0x60, 0x66, 0x8C, 0x88, 0xCF, 0x2F, 0x83, 0x58, 0xBC,
    0xB2, 0x00, 0x0A, 0x11, 0x72, 0x43, 0xC5, 0xDE, 0xEF, 0xBB, 0x2C, 0xBF, 0x97, 0x79, 0x6B, 0x9C,
    0x10, 0x1E, 0x7C, 0x57, 0x0E, 0xDB, 0x1D, 0x61, 0x6E, 0xB5, 0xF9, 0x3D, 0x35, 0xE9, 0x5C, 0xD8,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x33, 0x55, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x7E, 0x50, 0x53, 0x50, 0x00, 0x02, 0x00, 0x00, 0x01, 0x01, 0x22, 0x74, 0x69, 0x66, 0x70, 0x73,
    0x70, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x33, 0x55, 0x00, 0x50, 0x34, 0x55, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x40, 0x67, 0x3D, 0x00, 0x50, 0x55, 0x0A, 0x01, 0x10, 0x00, 0x40, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x6B, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x4C, 0x6B, 0x3D, 0x00, 0xCC, 0xBB, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00,
};

// Minna no Sukkiri Demo(421KB)
unsigned char pspHeader_small[336] = {
	0x7E, 0x50, 0x53, 0x50, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x94, 0x14, 0x11, 0x00, 0x60, 0x90, 0x06, 0x00, 
	0xB8, 0x80, 0x09, 0x00, 0x18, 0xA6, 0x0B, 0x00, 0x70, 0xA1, 0x05, 0x00, 0x10, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x80, 0xAC, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x05, 0x0D, 0x00, 0x00, 0x00, 
	0x8A, 0x42, 0xAA, 0x13, 0xAE, 0x02, 0x9C, 0x16, 0x99, 0x19, 0x3E, 0xEF, 0xE1, 0xCD, 0xFB, 0xBC, 
	0xDA, 0x28, 0x6E, 0xA5, 0x62, 0x67, 0x71, 0xB2, 0x14, 0x12, 0xAB, 0x7E, 0x1C, 0x69, 0x3A, 0x7A, 
	0xB7, 0x40, 0x8E, 0x91, 0xB1, 0x4F, 0x36, 0xE7, 0x82, 0xF1, 0xFD, 0xB1, 0x50, 0x6D, 0x33, 0xB4, 
	0x09, 0x8F, 0x06, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0xCB, 0x8D, 0xC7, 0x1B, 0x2A, 0xAF, 0x3B, 0x09, 0x2A, 0x5B, 0x4F, 0x9E, 0xE8, 0xE2, 0xCA, 0x66, 
	0xF0, 0x05, 0xF3, 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0xE3, 0x40, 
	0x6E, 0x14, 0x13, 0xEA, 0xA1, 0x81, 0x64, 0x54, 0x57, 0xBE, 0xA2, 0x43, 0x26, 0x7E, 0x4D, 0x0C, 
	0x4F, 0xA6, 0x87, 0x6A, 0xEA, 0x0D, 0xEF, 0xBE, 0x27, 0xE8, 0x78, 0x2D, 0x10, 0x40, 0x05, 0x96
};

unsigned char kirkHeader_small[272] = {
	0x9D, 0xC4, 0x48, 0xA6, 0x0E, 0x3C, 0xB7, 0x40, 0x4F, 0x93, 0xFF, 0x56, 0x15, 0x08, 0x28, 0x71, 
	0x3E, 0x52, 0xB5, 0x89, 0xA0, 0x1C, 0xC9, 0xEF, 0x6E, 0x11, 0x0A, 0xC8, 0x28, 0x67, 0x77, 0x66, 
	0xF2, 0xB2, 0x69, 0xCC, 0x08, 0x8C, 0x53, 0xA7, 0xA7, 0x25, 0xF7, 0x2B, 0x84, 0x53, 0x15, 0x54, 
	0x2D, 0x4A, 0xAD, 0xB6, 0x52, 0x40, 0x17, 0xD7, 0xA7, 0xF4, 0xB9, 0x11, 0x17, 0xB7, 0x13, 0x9B, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x09, 0x8F, 0x06, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x7E, 0x50, 0x53, 0x50, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x94, 0x14, 0x11, 0x00, 0x60, 0x90, 0x06, 0x00, 
	0xB8, 0x80, 0x09, 0x00, 0x18, 0xA6, 0x0B, 0x00, 0x70, 0xA1, 0x05, 0x00, 0x10, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x80, 0xAC, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x05, 0x0D, 0x00, 0x00, 0x00
};

u8 in_buffer[1024*1024*10];
u8 out_buffer[1024*1024*10];

u8 kirk_raw[1024*1024*10];
u8 kirk_enc[1024*1024*10];
u8 elf[1024*1024*10];

typedef struct header_keys
{
    u8 AES[16];
    u8 CMAC[16];
}header_keys;

int load_elf(char *elff)
{
	FILE *fp = fopen(elff, "rb");

	if(fp == NULL) {
		return -1;
	}

	fseek(fp, 0, SEEK_END);
	int size = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	fread(elf, 1, size, fp);
	fclose(fp);

	return size;
}

int dumpFile(char *name, void *in, int size)
{
	FILE *fp = fopen(name, "wb");

	if(fp == NULL) {
		return -1;
	}

	fwrite(in, 1, size, fp);
	fclose(fp);

	return 0;
}

int get_kirk_size(u8 *key_hdr)
{
	int krawSize = *(u32*)(key_hdr+0x70);

	if(krawSize % 0x10) {
		krawSize += 0x10 - (krawSize % 0x10); // 16 bytes aligned
	}

	krawSize += 0x110;

	return krawSize;
}

int is_compressed(u8 *psp_header)
{
	if (*(u16*)(psp_header+6) == 1) {
		return 1;
	}

	return 0;
}

int get_elf_size(u8 *psp_header)
{
	return *(u32*)(psp_header+0x28);
}

int gzip_compress(u8 *dst, const u8 *src, int size)
{
	int ret;
	z_stream strm;
	u8 *elf_compress;
	const int compress_max_size = 10 * 1024 * 1024;

	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;

	elf_compress = malloc(compress_max_size);

	if(elf_compress == NULL) {
		return -1;
	}

	ret = deflateInit2(&strm, 9, Z_DEFLATED, 15+16, 8, Z_DEFAULT_STRATEGY);

	if(ret != Z_OK) {
		printf("%s: compress error\n", __func__);
		free(elf_compress);
		
		return -2;
	}

	strm.avail_in = size;
	strm.next_in = (void*)src;
	strm.avail_out = compress_max_size;
	strm.next_out = elf_compress;

	ret = deflate(&strm, Z_FINISH);

	if(ret == Z_STREAM_ERROR) {
		deflateEnd(&strm);
		printf("%s: compress error\n", __func__);
		free(elf_compress);

		return -3;
	}

	memcpy(dst, elf_compress, strm.total_out);
	deflateEnd(&strm);
	free(elf_compress);

	return 0;
}

int main(int argc, char **argv)
{
	header_keys keys;
	u8 rawkheaderBk[0x90];

	if(argc < 2)
	{
		printf("USAGE: [exe] [prx]\n");
		return 0;
	}

	memset(in_buffer, 0, sizeof(in_buffer));
	memset(out_buffer, 0, sizeof(out_buffer));
	memset(kirk_raw, 0, sizeof(kirk_raw));
	memset(kirk_enc, 0, sizeof(kirk_enc));
	memset(elf, 0, sizeof(elf));

	kirk_init();

	int elfSize = load_elf(argv[1]);

	if(elfSize < 0) {
		printf("Cannot open %s\n", argv[1]);

		return 0;
	}

	u8 *kirkHeader = kirkHeader_small;
	u8 *pspHeader = pspHeader_small;

	int krawSize = get_kirk_size(kirkHeader);

	if(elfSize > krawSize - 0x150) {
		kirkHeader = kirkHeader_big;
		pspHeader = pspHeader_big;
		krawSize = get_kirk_size(kirkHeader);

		if(elfSize > krawSize - 0x150) {
			printf("PRX SIGNER: Elf is to big\n");

			return 0;
		}
	}

	if (is_compressed(pspHeader)) {
		elfSize = get_elf_size(pspHeader);
		gzip_compress(elf, elf, elfSize);
	}

	memcpy(kirk_raw, kirkHeader, 0x110);
	memcpy(rawkheaderBk, kirk_raw, sizeof(rawkheaderBk));

	kirk_decrypt_keys((u8*)&keys, kirk_raw);
	memcpy(kirk_raw, &keys, sizeof(header_keys));
	memcpy(kirk_raw+0x110, elf, elfSize);

	if(kirk_CMD0(kirk_enc, kirk_raw, sizeof(kirk_enc), 0) != 0)
	{
		printf("PRX SIGNER: Could not encrypt elf\n");
		return 0;
	}

	memcpy(kirk_enc, rawkheaderBk, sizeof(rawkheaderBk));

	if(kirk_forge(kirk_enc, sizeof(kirk_enc)) != 0)
	{
		printf("PRX SIGNER: Could not forge cmac block\n");

		return 0;
	}

	memcpy(out_buffer, pspHeader, 0x150);
	memcpy(out_buffer+0x150, kirk_enc+0x110, krawSize-0x110);

	return dumpFile("./data.psp", out_buffer, (krawSize-0x110)+0x150);
}
